package cn.wangsy.zhidao.p20170112;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTest {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");

		//获取数据库连接
		String user = "root" ,password = "mysql",url="jdbc:mysql://localhost:3306/test??characterEncoding=utf8";
		Connection connection = DriverManager.getConnection(url, user, password);
			
		//添加用户
		String sql = "insert into t1(id,name,password) values(?,?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setString(1, "123123123");
		statement.setString(2, "jdbc");
		statement.setString(3, "password");
		System.out.println(statement.executeUpdate());
		
		//登录
		sql = "select * from t1 where name = ? and password = ?";
		PreparedStatement query = connection.prepareStatement(sql);
		query.setString(1, "jdbc");
		query.setString(2, "jdbc");
		ResultSet rs = query.executeQuery();
		if(rs.next()){
			System.out.println("登录成功！");
		}else{
			System.out.println("登录失败！");
		}
		
		connection.close();
		
	}

}
